package edu.hrbu.spring.controller;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import edu.hrbu.spring.pojo.Dept;
import edu.hrbu.spring.pojo.UserBean;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author 徐登宇
 */
@RequestMapping("/test")
@Controller
public class TestController {

    @RequestMapping("/converterDate")
    public String testConverterDate(UserBean userBean) {
        System.out.println("【converterDate】@@@UserBean::"+userBean);
        return "hello";
    }

    @ResponseBody
    @RequestMapping("/accjson")
    public void testAccJson(@RequestBody UserBean userBean) throws JsonProcessingException {

        ObjectMapper objectMapper = new ObjectMapper();
        // 先将请求体获取到的对象转成Json（date数据会被转化）
        String userBeanJson = objectMapper.writeValueAsString(userBean);
        // 再讲Json转化回对象，此时日期是转化后的格式
        UserBean userBeanJsonParse = objectMapper.readValue(userBeanJson, UserBean.class);
        System.out.println("【accjson】@@@UserBean::"+userBeanJsonParse);
    }

    @ResponseBody
    @RequestMapping("/jackson")
    public String testJackson() throws JsonProcessingException {
        Dept dept = new Dept(1, "name", "loc", 1, 1);
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(dept);
    }

    @ResponseBody
    @RequestMapping(value = "/fastjson")
    public String testFastjson() {
        Dept dept = new Dept(1, "name", "loc", 1, 1);
        return JSON.toJSONString(dept);
    }

    @RequestMapping("/demo")
    public String demo() {
        return "/hello";
    }

}
